package model;

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyleConstants;

public class Formatar {
	
	SimpleAttributeSet recuo = new SimpleAttributeSet();
	float rec=0;
	
	public void tipoFonte(JTextPane texto, String tipo)
	{
		StyledDocument doc = texto.getStyledDocument();
		SimpleAttributeSet fontType = new SimpleAttributeSet();
		
		if (tipo == "Arial") {
			StyleConstants.setFontFamily(fontType, "Arial");
		}
		if (tipo == "Comic Sans MS") {
			StyleConstants.setFontFamily(fontType, "Comic Sans MS");
		}
		if (tipo == "Times New Roman") {
			StyleConstants.setFontFamily(fontType, "Times New Roman");
		}
		if (tipo == "Verdana") {
			StyleConstants.setFontFamily(fontType, "Verdana");
		}
		
		//texto.setCharacterAttributes(fontType, false);
		doc.setCharacterAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), fontType, false);
	}

	public void negrito(JTextPane texto)
	{
		SimpleAttributeSet bold = new SimpleAttributeSet();
		
		if (StyleConstants.isBold(texto.getCharacterAttributes())) 
		{
			StyleConstants.setBold(bold, false);
		}
		else 
		{
			StyleConstants.setBold(bold, true);
		}
		
		texto.setCharacterAttributes(bold, false);
		
	}
	
	public void italico(JTextPane texto)
	{
		SimpleAttributeSet italic = new SimpleAttributeSet();
		
		if (StyleConstants.isItalic(texto.getCharacterAttributes())) 
		{
			StyleConstants.setItalic(italic, false);
		}
		else 
		{
			StyleConstants.setItalic(italic, true);
		}
		
		texto.setCharacterAttributes(italic, false);
	}
	
	public void sublinhado(JTextPane texto)
	{
		SimpleAttributeSet underline = new SimpleAttributeSet();
		
		if (StyleConstants.isUnderline(texto.getCharacterAttributes())) 
		{
			StyleConstants.setUnderline(underline, false);
		}
		else 
		{
			StyleConstants.setUnderline(underline, true);
		}
		
		texto.setCharacterAttributes(underline, false);
	}
	
	public void corTexto(JTextPane texto, String cor)
	{
		StyledDocument doc = texto.getStyledDocument();
		SimpleAttributeSet color = new SimpleAttributeSet();
		
		if (cor == "Preto") {
			StyleConstants.setForeground(color, Color.BLACK);
		}
		if (cor == "Azul") {
			StyleConstants.setForeground(color, Color.BLUE);
		}
		if (cor == "Vermelho") {
			StyleConstants.setForeground(color, Color.RED);
		}
		if (cor == "Verde") {
			StyleConstants.setForeground(color, Color.GREEN);
		}
		
		doc.setCharacterAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), color, false);
	}
	
	public void tamanho(JTextPane texto, String tam)
	{
		StyledDocument doc = texto.getStyledDocument();
		SimpleAttributeSet size = new SimpleAttributeSet();
		
		if (tam == "8") {
			StyleConstants.setFontSize(size, 8);
		}
		if (tam == "12") {
			StyleConstants.setFontSize(size, 12);
		}
		if (tam == "16") {
			StyleConstants.setFontSize(size, 16);
		}
		if (tam == "20") {
			StyleConstants.setFontSize(size, 20);
		}
		
		//texto.setCharacterAttributes(size, false);
		doc.setCharacterAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), size, false);
	}
	
	public void espLinha(JTextPane texto, String esp)
	{
		StyledDocument doc = texto.getStyledDocument();
		SimpleAttributeSet lineSpace = new SimpleAttributeSet();
		float aux;
		
		if (esp == "0,0") {
			aux = 0.0f;
			StyleConstants.setLineSpacing(lineSpace, aux);
		}
		if (esp == "1,0") {
			aux = 1.0f;
			StyleConstants.setLineSpacing(lineSpace, aux);
		}
		if (esp == "1,15") {
			aux = 1.15f;
			StyleConstants.setLineSpacing(lineSpace, aux);
		}
		if (esp == "1,5") {
			aux = 1.5f;
			StyleConstants.setLineSpacing(lineSpace, aux);
		}
		if (esp == "2,0") {
			aux = 2.0f;
			StyleConstants.setLineSpacing(lineSpace, aux);
		}
		
		//texto.setParagraphAttributes(lineSpace, false);
		doc.setParagraphAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), lineSpace, false);
	}
	
	public void justificar(JTextPane texto)
	{
		SimpleAttributeSet center = new SimpleAttributeSet();
		StyleConstants.setAlignment(center, StyleConstants.ALIGN_JUSTIFIED);
		texto.setParagraphAttributes(center, false);
	}
	
	public void centralizar(JTextPane texto)
	{
		SimpleAttributeSet center = new SimpleAttributeSet();
		StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
		texto.setParagraphAttributes(center, false);
	}
	
	public void esquerda(JTextPane texto)
	{
		SimpleAttributeSet center = new SimpleAttributeSet();
		StyleConstants.setAlignment(center, StyleConstants.ALIGN_LEFT);
		texto.setParagraphAttributes(center, false);
	}
	
	public void direita(JTextPane texto)
	{
		SimpleAttributeSet center = new SimpleAttributeSet();
		StyleConstants.setAlignment(center, StyleConstants.ALIGN_RIGHT);
		texto.setParagraphAttributes(center, false);
	}
	
	public void cabecalho(JTextPane texto)
	{
		rec = StyleConstants.getLeftIndent(recuo);
		if (rec > 0) 
		{
        	StyleConstants.setLeftIndent(recuo, rec+30);
        }
        else 
        {
        	StyleConstants.setLeftIndent(recuo, 30);
        }
        
        texto.setParagraphAttributes(recuo, false);
        //doc.setParagraphAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), recuo, false);
	}
	
	public void rodape(JTextPane texto)
	{
		rec = StyleConstants.getLeftIndent(recuo);
		if (rec > 0) 
		{
            StyleConstants.setLeftIndent(recuo, rec-30);
        }
        texto.setParagraphAttributes(recuo, false);
        //doc.setParagraphAttributes(texto.getSelectionStart(), texto.getSelectedText().length(), recuo, false);
	}
	
}
